/**
 * Created by Administrator on 2015-8-3.
 */
    function setCookie(cname, cvalue, exdays) {
        var d = new Date();
        d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
        var expires = "expires=" + d.toUTCString();
        document.cookie = cname + "=" + cvalue + "; " + expires+";path=/";
    }

//获取cookie
    function getCookie(cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1);
            if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
        }
        return "";
    }


//清除cookie
    function clearCookie(name) {
        setCookie(name, "", -1);
    }

    function checkCookie() {
        var user = getCookie("username");
        if (user != "") {
            alert("Welcome again " + user);
        } else {
            user = prompt("Please enter your name:", "");
            if (user != "" && user != null) {
                setCookie("username", user, 365);
            }
        }
    }

    function setskin(name) {
        if (name) {
            var skin = getCookie("skin");
            if (name == skin)return;
            setCookie("skin", name, 360);
        }
        loadskin();
        var ddss = document.getElementsByTagName("iframe");
        for (var i = 0; i < ddss.length; i++) {
            var fr = ddss[i];
            if (fr.contentWindow.setskin) {
                fr.contentWindow.setskin();
            }
        }
    }

    function loadskin() {
        var skin = getCookie("skin");
        skin = skin ? skin : 'default';
        var _theme = document.getElementById("theme");
        var scripts = document.getElementsByTagName("script");
        var url ="";
        for(var i=0;i<scripts.length;i++){
            if(scripts[i].src.indexOf("lib/ladder-skin.js")>-1){
                url =scripts[i].src;
                url=url.replace(/\?[\w\W]*$/,"");
                break;
            }
        }
        url = url.replace('ladder-skin.js',"");
        if (!_theme) {
            var style = document.createElement('link');  //创建一个style元素
            var head = document.head || document.getElementsByTagName('head')[0]; //获取head元素
            style.type = 'text/css'; //这里必须显示设置style元素的type属性为text/css，否则在ie中不起作用
            style.rel = 'stylesheet';
            head.appendChild(style); //把创建的style元素插入到head中
            style.id = "theme";
            //style.href="../css/skin/default.css";
            head.appendChild(style);
            _theme = style;
        }
        switch (skin) {
            case "default":
                _theme.href =url+"../css/skin/bootstrap.css";//window.YFLib.ServerUrl;
                //_theme.href =ur+"css/skin/default.css";//window.YFLib.ServerUrl;
                break;
            case "bootstrap":
                _theme.href =url+"../css/skin/bootstrap.css";//window.YFLib.ServerUrl;
                break;
            case "black":
                _theme.href = url+"../css/skin/black.css";
                break;
            case "blue":
                _theme.href = url+"../css/skin/blue.css";
                break;
            default :
                _theme.href = url+"../css/skin/default.css";
        }
    }
    loadskin();


